/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.isel.medknow.tables.api;

import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import pt.isel.medknow.HibernateUtil;
import pt.isel.medknow.tables.Filial;

/**
 *
 * @author fallen
 */
public class FilialMethods {

    public static void testMethods() {
        
        FilialMethods fm = new FilialMethods();

        //Adding filials
//        fm.addFilial("Filial_1");
//        fm.addFilial("Filial_2");
//        fm.addFilial("Filial_3");
        
        //Listing added books
        fm.listFilials();

        //Getting book details for a particular book name.
        fm.getFilialDetails("Filial_1");

        //To see no. of books
        fm.countFilials();

        //Listing available books
        fm.listFilials();

        //Deleting a book
        fm.deleteFilial("Filial_1");

        //Listing available books After executing delete
        fm.listFilials();
    }
    //Using persistent objects to store data into the database.

    public void addFilial(String filialName) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Filial filial = new Filial();
        filial.setName(filialName);
        session.save(filial);
        session.getTransaction().commit();
    }

    //Using persistence object to delete a row from the database.
    public void deleteFilial(String filialName) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List list = session.createQuery("from Filial where name='" + filialName + "'").list();
        Iterator itr = list.iterator();
        while (itr.hasNext()) {
            Filial filial = (Filial) itr.next();
            System.out.println("delete : " + filial);
            session.delete(filial);
        }
        session.getTransaction().commit();
    }

    /*
    //Using Criteria API to update the book cost
    public void updateBook(String bookName, int bookCost) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Criteria cri = session.createCriteria(BookDetails.class);
        cri = cri.add(Restrictions.eq("bookName", bookName));
        List list = cri.list();
        BookDetails bd = (BookDetails) list.iterator().next();
        bd.setBookCost(bookCost);
        session.update(bd);
        session.getTransaction().commit();
    }
    */

    //Using HQL - Hibernate Query Language
    public void getFilialDetails(String filialName) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Query q = session.createQuery("from Filial where name=:fn");
        q.setString("fn", filialName);
        List list = q.list();
        System.out.println("Getting filial Details using HQL. \n" + list);

        //The above query can also be achieved with Criteria & Restrictions API.
        Criteria cri = session.createCriteria(Filial.class);
        cri = cri.add(Restrictions.eq("name", filialName));
        list = cri.list();
        System.out.println("Getting filial Details using Criteria API. \n" + list);

        session.getTransaction().commit();
    }

    //Aggregate function.
    public void countFilials() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List list = session.createQuery("select count(*) from Filial").list();
        System.out.println("Aggregate function count \n" + list);
        session.getTransaction().commit();
    }

    //Native SQL Query
    public void listFilials() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List list = session.createSQLQuery("select * from filials").addEntity(Filial.class).list();
        Iterator itr = list.iterator();
        while (itr.hasNext()) {
            Filial filial = (Filial) itr.next();
            System.out.println(filial);
        }
        session.getTransaction().commit();

    }
}
